// pages/scan-main/scan-main.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    borrowMsg: {}
  },
  
  scanBorrowBtn: function (e) {
    let that = this;
    wx.scanCode({
      success: function (res1) {
        var timestamp = new Date().getTime();
        var msg = JSON.stringify(res1.result);
        if (msg.indexOf("book-borrow-id:") != -1) {
          var borrowId1 = msg.replace("book-borrow-id:", "");
          var reg = new RegExp('"', "g");
          borrowId1 = borrowId1.replace(reg, "");
          console.log("borrowId1:" + borrowId1);
          util.request(api.getBorrow, { borrowId: borrowId1}).then(function(res){
            if(res.errno == 0){
              console.log("返回成功");
            }
            that.setData({ borrowMsg: res.data.borrow});
            console.log(that.data.borrowMsg);
            var borrowBookName = that.data.borrowMsg.bookName;
            //var borrowUserName = that.data.borrowMsg.user.username;
            var borrowUserName = "wong华炎";
            console.log("borrowBookName" + borrowBookName + ",borrowBookName:" + borrowBookName);
            //判断二维码是否过期
            var borrowStartTime = that.data.borrowMsg.startTime;
            console.log("borrowStartTime: " + borrowStartTime);
            console.log("timestamp:" + timestamp);
            console.log(timestamp - borrowStartTime);
            if (timestamp - borrowStartTime > 3000000000) {
              wx.showModal({
                title: '借阅提示',
                content: '该验证码已过期,请重新扫码借书',
                success: function (res) {
                  if (res.confirm) {
                  } else if (res.cancel) {
                  }
                }
              })
            } else {
              wx.showModal({
                title: '借阅提示',
                content: '是否允许将《' + borrowBookName + '》借给' + borrowUserName,
                success: function (res) {
                  if (res.confirm) {
                    console.log("api.IndexRootUrl:" + api.IndexRootUrl);
                    console.log("api.IndexRootUrl1:"+api.IndexRootUrl + 'api-scan-allow-borrow');
                    //请求批准借阅接口
                    util.request(api.IndexRootUrl + 'scan/api-scan-allow-borrow', { borrowId: borrowId1}).then(function(res){
                      console.log("res:" + res);
                    });
                  } else if (res.cancel) {
                  }
                }
              })
            }
            });  
        }
        /*if (msg.indexOf("book-borrow2-id:") != -1) {
          var borrowId = msg.replace("book-borrow2-id:", "");
          var reg = new RegExp('"', "g");
          borrowId = borrowId.replace(reg, "");
          wx.request({
            url: getApp().globalData.url + 'api-scan-borrow-byid/' + borrowId,
            data: {},
            method: 'GET',
            success: function (res) {
              that.setData({ borrowMsg: res.data.borrow });
              console.log(that.data.borrowMsg);
              var borrowBookName = that.data.borrowMsg.book.bookName;
              var borrowUserName = that.data.borrowMsg.user.userUsername;
              //判断二维码是否过期
              var borrowStartTime = that.data.borrowMsg.borrowStartTime;
              console.log("borrowStartTime: " + borrowStartTime);
              console.log("timestamp:" + timestamp);
              console.log(timestamp - borrowStartTime);
              if (timestamp - borrowStartTime > 90000000) {
                wx.showModal({
                  title: '借阅提示',
                  content: '该验证码已过期,请重新扫码借书',
                  success: function (res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                  }
                })
              } else {
                wx.showModal({
                  title: '借阅提示',
                  content: '是否允许将《' + borrowBookName + '》借给' + borrowUserName,
                  success: function (res) {
                    if (res.confirm) {
                      //请求批准借阅接口
                      wx.request({
                        url: getApp().globalData.url + 'api-scan-allow-borrow/' + borrowId,
                        data: {},
                        method: 'GET',
                        success: function (res) {
                        }
                      })
                    } else if (res.cancel) {
                    }
                  }
                })
              }
            }
          })
        }*/
      }
    })
  },
  scanReturnBtn: function (e) {
    var that = this;
    wx.scanCode({
      success: function (res) {
        var timestamp = new Date().getTime();
        var times = util.formatTime(timestamp);
        console.log("times:"+times);
        var msg = JSON.stringify(res.result);
        if (msg.indexOf("book-return-id:") != -1) {
          var borrowId = msg.replace("book-return-id:", "");
          var reg = new RegExp('"', "g");
          borrowId = borrowId.replace(reg, "");
          util.request(api.getBorrow, { borrowId: borrowId }).then(function (res) {
            that.setData({ borrowMsg: res.data.borrow });
            console.log(that.data.borrowMsg);
            var borrowBookName = that.data.borrowMsg.bookName;
            //var borrowUserName = that.data.borrowMsg.user.userName;

            var start_date = new Date(res.data.borrow.endTime);
            var dates = timestamp - start_date.getTime();
            var day = parseInt(dates / (1000 * 60 * 60 * 24));  
            //var dates1 = util.GetDateDiff(res.data.borrow.endTime, timestamp);
            if(day!=0){
              wx.showModal({
                title: '借阅提示',
                content: '该图书逾期' + day + '天，将以1元/天扣费情况进行扣费，总共扣费：' + day + '元,点击确认会进行扣费并归还图书',
                success: function (res){
                  if(res.confirm) {
                    //请求批准还书接口
                    util.request(api.IndexRootUrl + 'scan/api-scan-allow-return', { borrowId: borrowId ,delayDay:day})
                      .then(function (res) {
                        console.log("res:" + res);
                    })
                   
                  } else if (res.cancel) {
                  }
                }
              })
            }else{
              wx.showModal({
                title: '借阅提示',
                content: '确认归还《' + borrowBookName + '》吗?',
                success: function (res) {
                  if (res.confirm) {
                    //请求批准还书接口
                    util.request(api.IndexRootUrl + 'scan/api-scan-allow-return', { borrowId: borrowId, delayDay: day}).then(function (res) {
                      console.log("res:"+res);
                    })
                  } else if (res.cancel) {
                  }
                }
              });
            }
          })
        }
        /*if (msg.indexOf("book-return2-id:") != -1) {
          var borrowId = msg.replace("book-return2-id:", "");
          var reg = new RegExp('"', "g");
          borrowId = borrowId.replace(reg, "");
          wx.request({
            url: getApp().globalData.url + 'api-scan-borrow-byid/' + borrowId,
            data: {},
            method: 'GET',
            success: function (res) {
              that.setData({ borrowMsg: res.data });
              console.log(that.data.borrowMsg);
              var borrowBookName = that.data.borrowMsg.book.bookName;
              var borrowUserName = that.data.borrowMsg.user.userUsername;
              wx.showModal({
                title: '借阅提示',
                content: '确认归还《' + borrowBookName + '》吗?',
                success: function (res) {
                  if (res.confirm) {
                    //请求批准借阅接口
                    wx.request({
                      url: getApp().globalData.url + 'api-scan-allow-return/' + borrowId,
                      data: {},
                      method: 'GET',
                      success: function (res) {
                      }
                    })
                  } else if (res.cancel) {
                  }
                }
              })
            }
          })
        }*/
      }
    })
  },
  
})